/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.ListCellRenderer;
import org.apache.log4j.Logger;
import sk.yw.azetclient.model.Buddy;

/**
 *
 * @author error216
 */
public class BuddyCellRenderer implements ListCellRenderer {
    
    private static final Logger logger =
            Logger.getLogger(BuddyCellRenderer.class);
    
    public BuddyCellRenderer() {
    }
    
    public Component getListCellRendererComponent(JList list,
            Object value, int index, boolean isSelected, boolean cellHasFocus) {
        JLabel component = new JLabel();

        if (value == null) {
            component.setText("null");
        } else if (!(value instanceof Buddy)) {
            component.setText(value.toString());
        } else {
            Buddy buddy = (Buddy) value;
            if (buddy.equals(BuddyListModel.getSeparator())) {
                return new JSeparator();
            } else {
                component.setText(buddy.getVisualName());
                switch(buddy.getStatus()) {
                    case CHATTING:
                        component.setFont(getChattingFont(list));
                        break;
                    case ONLINE:
                        component.setFont(getOnlineFont(list));
                        break;
                    case OFFLINE:
                        component.setFont(getOfflineFont(list));
                        break;
                }
            }
        }
        
        component.setOpaque(true);
        if (isSelected) {
            component.setBackground(list.getSelectionBackground());
            component.setForeground(list.getSelectionForeground());
        } else {
            component.setBackground(list.getBackground());
            component.setForeground(list.getForeground());
        }

        return component;
    }
    
    private Font getChattingFont(Component base) {
        return base.getFont().deriveFont(Font.BOLD);
    }
    
    private Font getOnlineFont(Component base) {
        return base.getFont().deriveFont(Font.BOLD | Font.ITALIC);
    }
    
    private Font getOfflineFont(Component base) {
        return base.getFont().deriveFont(Font.PLAIN);
    }
    
}
